{* $Id$ *}
{include_core file="admin/lib/header.html" title="属性展示"}
<div class="page-header border-bottom clearfix">
  <h2 class="left">属性管理<span>{if $property_info}编辑{else}添加{/if}属性</span></h2>
  <a href="{AnUrl('property/admin')}" class="btn btn-default btn-sm right">&lt;&nbsp;返回属性列表</a>
</div>
<!--start:属性新增-->
<div class="add-form">
  <form class="js_creatForm" action="{AnUrl('property/admin/save')}" method="post" enctype="multipart/form-data">
    <input type="hidden" name="id" value="{$property_info.id}">
    <table class="table" cellpadding="0" cellspacing="0">
      <tr>
        <th><label for="" class="add-f-label">所属分类：</label></th>
        <td>
          <select class="add-f-slt1 js_type" name="info[category_id]">
            <option value="0" {if $property_info.category_id == 0}selected{/if}>全局属性</option>
            {$category_list = category_helper::get_category_list_by_parent_id(0,'goods')}
            {foreach $category_list as $k=>$v}
            <option value="{$v.id}" {if $property_info.category_id == $v.id}selected{/if}>{$v.title}</option>
            {/foreach}
          </select>
          <div class="tips"><span style="line-height: 34px;color: #f00;margin-right: 10px;">*</span>请选择属性类型</div>
        </td>
      </tr>
      <tr>
        <th><label for="" class="add-f-label">属性标题：</label></th>
        <td>
          <input name="info[title]" style="width:490px;" value="{$property_info.title}" type="text" class="add-f-ipt1 js_title">
          <div class="tips"><span style="line-height: 34px;color: #f00;margin-right: 10px;">*</span>属性标题会显示在相应属性的标题位置，最多输入 <span style="color:red;">20</span> 字，还可输入 <span class="js_titleNum" style="color:red;">20</span> 字</div>
        </td>
      </tr>
      <tr>
        <th><label for="" class="add-f-label">属性值：</label></th>
        <td class="js_valueGroup">
          {$property_value_list = property_helper::get_property_value_list_by_property_id($property_info.id)}
          {if $property_value_list}
          {foreach $property_value_list as $k=>$v}
          <span><input name="info[value][{$v.id}]" style="width:450px;" value="{$v.value}" type="text" class="add-f-ipt1 js_value" id-data="{$v.id}"><b class="btn btn-danger btn-xs js_delGroup" style="margin-left:10px;">×</b><br /><br /></span>
          {/foreach}
          {/if}
          <span><input name="info[value][]" style="width:450px;" type="text" class="add-f-ipt1 js_value" id-data=""><b class="btn btn-danger btn-xs js_delGroup" style="margin-left:10px;">×</b><br /><br /></span>
          <span class="btn btn-default btn-sm js_addGroup">&nbsp;&nbsp;+添加属性&nbsp;&nbsp</span>
        </td>
      </tr>
      <tr><th>&nbsp;</th><td class="noborder"><button type="submit" class="btn btn-success js_submit">&nbsp;&nbsp;&nbsp;确认&nbsp;&nbsp;&nbsp;</button></td></tr>
    </table>
  </form>
</div>
<!--end:属性新增-->
<script>
var resName = 'property';
function setNum(thisClass, maxNum) {
  var presentNum = $('.'+thisClass).val().length;
  var remainNum  = maxNum - presentNum;
  if (remainNum < 0) {
    $('.'+thisClass).val($('.'+thisClass).val().substr(0,maxNum));
    $('.'+thisClass+'Num').text(0);
  } else {
    $('.'+thisClass+'Num').text(remainNum);
  }
}

$(function(){
  // 初始化剩余字数
  setNum('js_title', 20);
  $('.js_title').keyup(function(){
    setNum('js_title', 20);
  });
  
  // 添加属性值 input
  $('.js_addGroup').click(function(){
    $(this).before('<span><input name="info[value][]" style="width:450px;" type="text" class="add-f-ipt1 js_value" id-data=""><b class="btn btn-danger btn-xs js_delGroup" style="margin-left:10px;">×</b><br /><br /></span>');
  });

  // 删除属性值 input
  $('.js_valueGroup').on('click', '.js_delGroup', function(){
    var propertyValueId = $(this).parent().children().eq(0).attr('id-data');
    var thisInput = $(this);
    if (propertyValueId) {
      $.get("{AnUrl('property/admin/check_proerty_value')}", { property_value_id:propertyValueId }, function(data){
        if (data.info == 'ok') {
          showMsg('当前属性值已经被一些商品所使用，不能删除，但可以修改名称！');
        } else {
          thisInput.parent().remove();
        }
      }, 'json');
    } else {
      thisInput.parent().remove();
    }    
  });

  // 提交验证
  $('.js_submit').click(function(e){
    e.preventDefault();
    var title      = $.trim($('.js_title').val());
    if (!title) {
      showMsg('标题不能为空');
      return;
    }

    $('.js_creatForm').submit();
  });
});
</script>
{include_core file="admin/lib/footer.html"}